<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
include ("../../include/dbFunctions.php");
include ("../../include/image_slider.php");
include ("../../include/function.php");

if (isset($_SESSION['school_id'])) {
    $rep_id = $_SESSION['school_id'];


    $created_date = date("y-m-j H:i:s ");
    $event_date = date("y-m-j");

    $evendatetarray = explode("-", $event_date);
    $event_date = "$evendatetarray[2]$evendatetarray[1]$evendatetarray[0]";



// checking fields in term of empty of SQL injection
//  $empty_array = array('start_date', 'end_date', 'event_remarks');
// $empty = check_empty($empty_array);
// Get input data from schoolRequestEvent page

    $date_array = $_POST['date_array'];

    $time_array = $_POST['time_array'];
    $end_time_array = $_POST['end_time_array'];
    
    $program_id = $_POST['program_id'];
    $program = "SELECT * FROM program WHERE program_id = $program_id";
    $program = executeSelectQuery($program);

    $no_class = $_POST['no_class'];
    $class_level = $_POST['class_level'];
    $no_session = $_POST['no_session'];

    $class_name = array();
    $no_student = array();
    

    for ($i = 0; $i < $no_class; $i++) {

        $class_name[] = htmlspecialchars($_POST['class_name' . $i . ''], ENT_QUOTES);
        $no_student[] = $_POST['no_student' . $i . ''];
        
    }
//$end_date = explode("/", $end_date);
//$final_end_date = "$end_date[2]-$end_date[0]-$end_date[1]";
//$compare_date = compare_date($final_start_date, $final_end_date);


    $school_name = "SELECT s.school_name, s.school_id FROM school s, contact_school c WHERE s.school_id = c.school_id AND c.contact_school_id = $rep_id ";
  
    $school_name = executeSelectQuery($school_name);
    
    $id_school = $school_name[0]['school_id'];
    
    $event_name = $event_date . '-' . $program[0]['program_name'] . '-' . $school_name[0]['school_name'];
 
// Insert into event table
    $insert_event = "INSERT INTO event (event_name, event_status, contact_id, program_id, created_date, last_modified, event_class_level, school_id)
                 VALUES('$event_name', 'requested', $rep_id, $program_id, '$created_date', '$created_date', '$class_level', $id_school)
                ";
    $insert_event = executeQuery($insert_event);

    if ($insert_event == 1) {
// select new event id
        $true = 0;
        $event_id = "SELECT event_id FROM event WHERE event_name = '$event_name'";
        $event_id = executeSelectQuery($event_id);
        
        $track_id = "SELECT * FROM track_criteria";
        $track_id = executeSelectQuery($track_id);
        
        for ($i = 0; $i < count($track_id); $i++){
            $insert_track = "INSERT INTO track_detail (event_id, track_id) VALUES (".$event_id[0]['event_id'].", ". $track_id[$i]['track_id'].")";
            $insert_track = executeSelectQuery($insert_track);
            
        }
        
        
        if ($event_id != 0) {

            $session = 0;
            $class = 0;

            for ($i = 0; $i < $no_session; $i++) {

                $date = explode("/", $date_array[$i]);

                $final_date = "$date[2]-$date[0]-$date[1]";

                $insert_session = "INSERT INTO session (session_date, session_time,session_end_time, event_id) 
                                    VALUES('$final_date','$time_array[$i]', '$end_time_array[$i]'," . $event_id[0]['event_id'] . ")";
                $insert_session = executeQuery($insert_session);
                if ($insert_session) {
                    $session = $session + 0;
                } else {
                    $session = $session + 1;
                }
            }



            for ($i = 0; $i < $no_class; $i++) {
                if (isset($_FILES['file' . $i . ''])) {


                    $filename = strtolower($_FILES['file' . $i . '']['name']);
                    $exts = explode(".", $_FILES['file' . $i . '']['name']);
                    $n = count($exts) - 1;
                    $ext = $exts[$n];

                    $ran = rand();
                    $ran2 = $ran . ".";
                    $target = "../../admin/upload/";
                    $target = $target . $ran2 . $ext;

                    move_uploaded_file($_FILES['file' . $i . '']['tmp_name'], $target);
                    $volunteer_cv = $ran2 . $ext;


                    /* $target_path = "../../admin/upload/";
                      $volunteer_cv = $_FILES['file' . $i . '']['name'];
                      $target_path = $target_path . basename($_FILES['file' . $i . '']['name']);
                      move_uploaded_file($_FILES['file' . $i . '']['tmp_name'], $target_path);

                     */


                    $insert_class = "INSERT INTO class(class_name, class_no_student,class_student_list, event_id)
                               VALUES('$class_name[$i]', '$no_student[$i]', '$volunteer_cv', " . $event_id[0]['event_id'] . ")";

                    $insert_class = executeQuery($insert_class);
                    if ($insert_class) {
                        $class = $class + 0;
                    } else {
                        $class = $class + 1;
                    }
                }
            }

            if ($session == 0 && $class == 0) {

                $message = "<p class='pmain'> You have successfully request new course!Thank you for your support of JA programs!<br></p>";

                $message = "<p class='pmain'>You have successfully requested for a new course!  Thank you for your support of JA programs!<br></p>";

            } elseif ($class != 0) {
                $message = "<p class='pmain'>Problem occur when insert class details, please try again!<br></p>";
                $message.= "<center><input type='button' class='form-back value='&nbsp' onClick='javascript:history.go(-1)'/></center>";
            } elseif ($session != 0) {
                $message = "<p class='pmain'>Problem occur when insert session details, please try again!<br></p>";
                $message.= "<center><input type='button' class='form-back value='&nbsp' onClick='javascript:history.go(-1)'/></center>";
            } else {
                $message = "<p class='pmain'>Problem occur when insert class & session details, please try again!<br></p>";
                $message.= "<center><input type='button' class='form-back value='&nbsp' onClick='javascript:history.go(-1)'/></center>";
            }
        } else {
            $message = "<p class='pmain'>Problem occur when retrieve course id, please try again!<br></p>";
            $message.= "<center><input type='button' class='form-back value='&nbsp' onClick='javascript:history.go(-1)'/></center>";
        }
    } else {
        $message = "<p class='pmain'>Problem occur when insert course's details, please try again!<br></p>";
        $message.= "<center><input type='button' class='form-back value='&nbsp' onClick='javascript:history.go(-1)'/></center>";
    }
}
?>

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Junior Achievement Singapore</title>
        <link href="../../media/css/styles.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" language="javascript" src="../../media/js/jquery/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" src="../../media/js/jquery/image_slider.js"></script>
        <!-- Custom jquery scripts -->
        <script src="../../media/js/jquery/custom_jquery.js" type="text/javascript"></script>
    </head>
    <body>
        <script>
            var timeout    = 500;
            var closetimer = 0;
            var ddmenuitem = 0;

            function jsddm_open()
            {  jsddm_canceltimer();
                jsddm_close();
                ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

            function jsddm_close()
            {  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

            function jsddm_timer()
            {  closetimer = window.setTimeout(jsddm_close, timeout);}

            function jsddm_canceltimer()
            {  if(closetimer)
                {  window.clearTimeout(closetimer);
                    closetimer = null;}}

            $(document).ready(function()
            {  $('#navigation > li').bind('mouseover', jsddm_open)
                $('#navigation > li').bind('mouseout',  jsddm_timer)});

            document.onclick = jsddm_close;

        </script>
        <div id="container"><!--CONTAINER STARTS-->
            <div id="navigation">
                <li><a href="../../include/home/homepage.php">HOME</a></li>
                <li><a href="../../include/home/contact.php">CONTACT</a></li>
                <li><a href="http://ja.org.sg/">ABOUT JA SINGAPORE</a></li>
                <li><a href="http://jascheduling.lefora.com/">FEEDBACK</a></li>
                <li><a href="../../guide/guide_menu.php">HELP</a></li>

            </div>
            <div id="title"><!--TITLE STARTS-->
            </div><!--TITLE ENDS-->
            <div id="featured"><!--FEATURED AREA STARTS-->
                <div class="featured-image"><!--FEATURED IMAGE STARTS-->
                    <img alt="Featured Image" src="../../media/images/featured_image.png"/>
                </div><!--FEATURED IMAGE ENDS-->
                <div class="featured-text"><!--FEATURED TEXT STARTS-->
                    <h2>Welcome to JA Singapore </h2>
                    <p>Junior Achievement programs help prepare young people for the real world by showing them how to generate wealth and effectively manage it, how to create jobs which make their communities more robust, and how to apply entrepreneurial thinking to the workplace. <a href="http://www.jaworldwide.org/">Read More... </a></p>
                </div><!--FEATURED TEXT ENDS-->
            </div><!--FEATURED AREA ENDS-->
            <?php
            custom_login();
            ?>
            <div id="left-content"><!--RIGHT CONTENT STARTS-->
                <div class="left-top"><!--RIGHT CONTENT BOX TOP-->
                </div><!--RIGHT CONTENT BOX TOP ENDS-->
                <div class="left-middle"><!--RIGHT CONTENT BOX MIDDLE-->
                    <?php
                    if (!isset($_SESSION['school_id'])) {
                        echo "<br/><br/><br/><br/><br/><center><h3>You don't have permission to view this page<h3><br/>
                      Please login to access the page.</center><br/><br/><br/><br/><br/><br/><br/><br/>";
                    } else {


                        echo "<br/><br/><br/><br/><br/><center><p class='pmain'>$message</p></center><br/>";

                        if ($true == 0 && $session == 0 && $class == 0) {
                            ?>
                            <br/><br/>
                            <center>  <h4>Click <a href ="representativeDetailEvent.php?id =<?php echo $event_id[0]['event_id'] ?>">here</a> to view new course. </h4>  </center><br/>
                              

                            <br/><br/><br/><br/><br/>
                            <?php
                        }
                    }
                    ?>
                </div><!--RIGHT CONTENT BOX MIDDLE ENDS-->
                <div class="left-bottom"><!--RIGHT CONTENT BOX BOTTOM-->
                </div><!--RIGHT CONTENT BOX BOTTOM ENDS-->
            </div><!--RIGHT CONTENT ENDS-->
        </div><!--CONTAINER ENDS-->

        <div id="footer"><!--FOOTER STARTS-->
            <p>Copyright &copy; Junior Achievement Singapore | All Rights Reserved</p>
        </div><!--FOOTER ENDS-->

    </body>
</html>


